package com.zjpavt.client.udp.udpservice;

import com.zjpavt.client.hal.DeviceID;
import com.zjpavt.client.hal.network.NetworkManager;
import com.zjpavt.client.hal.network.NetworkReceiveAndSendBytes;
import com.zjpavt.client.page.PageManager;
import com.zjpavt.client.util.CmdUtil;
import io.netty.channel.ChannelHandlerContext;

import java.io.IOException;
import java.util.Date;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 *
 *
 * 武汉演示使用  增加了一些临时功能 主要是额外的数据采集 设置时间和开关间隔时间
 * @author zyc
 */
public class WuhanDemostration implements UdpService {
    private final static Pattern DEVICE_ID_PATTERN = Pattern.compile("^deviceId=(?<deviceId>[0-9a-fA-F]{32}).*$");
    private final static Pattern CHANGE_TIME_PATTERN = Pattern.compile("^deviceId=(?<deviceId>[0-9a-fA-F]{32}) deviceTime=(?<time>[0-9]*)$");
    private final static Pattern TRIGGER_INTERVAL_PATTERN = Pattern.compile("^deviceId=(?<deviceId>[0-9a-fA-F]{32}) triggerInterval=(?<intgerval>[0-9]{1,5})$");
    @Override
    public void execute(byte[] commandBytes) {

    }

    @Override
    public void execute(String command) {

    }

    @Override
    public void execute(String msg, ChannelHandlerContext channel) {

        Matcher matcher = DEVICE_ID_PATTERN.matcher(msg);
        if (!isSend2Me(matcher)) {
            return;
        }
        String[] messages=  msg.split(" ");
//     deviceId=(?<deviceId>[0-9a-fA-F]{32}) query
        if ("query".equals(messages[1])) {
            StringBuilder stringBuilder = new StringBuilder();
            stringBuilder.append("deviceId=").append(DeviceID.getINSTANCE().getUUIDCached().toString().replaceAll("-", ""))
            .append(" singalStrength=")
            .append(NetworkManager.getInstance().getNetSignalStrength())
            .append(" messageSize=")
            .append(NetworkReceiveAndSendBytes.getMonthCount())
            .append(" powerFactor=")
            .append(1)
            .append(" power=")
            .append(456.3);
            channel.writeAndFlush(stringBuilder.toString());
            return;
        }
        Matcher changeTimeMatcher = CHANGE_TIME_PATTERN.matcher(msg.trim());
//      修改系统时间
        if (changeTimeMatcher.matches()) {
            long time = Long.parseLong(changeTimeMatcher.group("time"));
            try {
                CmdUtil.setDate(new Date(time));
            } catch (IOException e) {
                e.printStackTrace();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            return;
        }
        Matcher tiggerIntervalMatcher = TRIGGER_INTERVAL_PATTERN.matcher(msg.trim());
//      修改开关间隔时间
        if (tiggerIntervalMatcher.matches()) {
            int relTime = Integer.parseInt(tiggerIntervalMatcher.group("intgerval"));
            if (PageManager.getInstance().getTemperaturePage() != null) {
                PageManager.getInstance().getTemperaturePage().saveRelTime(relTime);
            }
            return;
        }
    }

    /**
     *
     * @param matcher
     * @return
     */
    private boolean isSend2Me(Matcher matcher) {
        if (matcher.matches()) {
            String deviceId = matcher.group("deviceId");
            String deviceId1 = DeviceID.getINSTANCE().getDeviceIdCached();
            if (deviceId .equals(deviceId1)) {
//				是发送到本机的数据
            } else {
                return false;
            }
        } else {
            return false;
        }
        return true;
    }
}
